package com.Ngbussiness.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Ngbussiness.dao.membersDao;
import com.Ngbussiness.dao.rechargeDao;
import com.Ngbussiness.dao.impl.membersDaoImpl;
import com.Ngbussiness.dao.impl.rechargeDaoImpl;
import com.Ngbussiness.util.Calenda;

public class getred_moneyServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
        int userid=Integer.parseInt(request.getParameter("userid"));
        int yongjinid=Integer.parseInt(request.getParameter("yongjinid"));
        Calenda cal=new Calenda();
        String time1=cal.getNowTime().toString();
        String time2=time1.substring(0, 10);
        membersDao m=new membersDaoImpl();
        rechargeDao rc=new rechargeDaoImpl();
        String b=m.checkqrm(userid,time2);
        if(b.equals("f1")){
        	out.print("yq five");
        }
        else{
        	   List list=rc.checkexist(userid,yongjinid,time2);
        	   System.out.println("c1"+list);
        	   if(list.size()>0){
               	out.print("yj yq");
               }
        	   else{
               	List list1=rc.insertrc(userid,yongjinid,time1);
               	HashMap adu=(HashMap)list1.get(0);
               	System.out.println(list1);
                   Double probability=Double.parseDouble(adu.get("probability").toString());
                   double price=Double.parseDouble(adu.get("price").toString());
                   int num=Integer.parseInt(adu.get("num").toString());
                   int ucnum=Integer.parseInt(adu.get("ucnum").toString());
                   int member_id=Integer.parseInt(adu.get("member_id").toString());
                   rc.getuc(userid,yongjinid,time1);
                   if(num>ucnum){
                   	Random r=new Random();
               		float floatNumber = r.nextFloat();
               		if(floatNumber>probability){
               			rc.getred(userid,yongjinid,time1);
               			rc.addmember(userid,price);
               			out.print(price);
               		}
               		else{
               			out.print("wei qiang dao");
               		}
                   }
                   else{
                   	out.print("red not");
                   }
                   
               }
        } 
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
            doGet(request,response);
	}

}
